package com.frank.loadinglibrary.common.gear;

import com.frank.loadinglibrary.ResourceTable;
import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.app.Context;

public class TwoGearsLoadingComponent extends DependentLayout {

    GearComponent firstGear;
    GearComponent secondGear;

    public GearComponent getFirstGear() {
        return firstGear;
    }

    public GearComponent getSecondGear() {
        return secondGear;
    }

    public TwoGearsLoadingComponent(Context context) {
        this(context, null);
    }

    public TwoGearsLoadingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public TwoGearsLoadingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initUI();
    }

    private void initUI() {
        Component loadingComponent = LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_layout_two_gears, this, true);
        firstGear = (GearComponent) loadingComponent.findComponentById(ResourceTable.Id_firstGear);

        secondGear = (GearComponent) loadingComponent.findComponentById(ResourceTable.Id_secondGear);

        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                firstGear.createAnimatorProperty().rotate(360).setDuration(2000).setLoopedCount(Animator.INFINITE).start();
                secondGear.createAnimatorProperty().rotate(-360).setDuration(2000).setLoopedCount(Animator.INFINITE).start();
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });

    }


}
